<?php
namespace app\index\controller;
use think\Exception;
use think\Request;

class Index extends Base 
{
    public function test() 
    {
        $phone = '18612566761';
        $config = \think\Config::get('login_sms');
        $loginKey = $config['login_key'].$phone;
        \think\Cache::set($loginKey, '111222', $config['login_expire']);//设置验证码
    }
    /**
     * 检查用户登录
     * @date: 2017年8月12日 上午12:53:09
     * @author: hxzlh
     * @return string|unknown
     * @return: return
     */
    public function check() 
    {
        try{
            $data = $this->checkLogin();
            return parent::returnTrue($data);
        } catch (Exception $e) {
            $message['code'] = $e->getCode();
            $message['mes'] = $e->getMessage();
            return parent::returnFalse($message);
        }
    }
    /**
     * 用户登录
     * @date: 2017年8月11日 下午10:58:49
     * @author: hxzlh
     * @throws Exception
     * @return string|unknown
     * @return: return
     */
    public function login() 
    {
        try{
            $phone = Request::instance()->post('phone', null, 'trim');//手机号
            $code = Request::instance()->post('code', null, 'trim');//验证码
            $openid = Request::instance()->post('openid', null, 'trim');//openid
            if (empty($phone) || empty($code) || empty($openid)) {
                throw new Exception('参数缺失', '102');
            }
            $sms = \think\Loader::model('Sms','service');
            $data = $sms->checkCode($phone, $code);//检查验证码
            
            $model = \think\Loader::model('Student','service');
            $data = $model->login($phone, $openid);
            return parent::returnTrue($data);
        } catch (Exception $e) {
            $message['code'] = $e->getCode();
            $message['mes'] = $e->getMessage();
            return parent::returnFalse($message);
        }
    }
    /**
     * 用户预约
     * @date: 2017年8月11日 下午11:20:24
     * @author: hxzlh
     * @throws Exception
     * @return string|unknown
     */
    public function appointment()
    {
        try{
            $phone = Request::instance()->post('phone', null, 'trim');//手机号
            $code = Request::instance()->post('code', null, 'trim');//验证码
            $name = Request::instance()->post('name', null, 'trim');//名称
            $openid = Request::instance()->post('openid', '', 'trim');//openid
            $comment = Request::instance()->post('comment', '', 'trim');//备注
            $course = Request::instance()->post('course/d', null, 'trim');//课程id
            $level = Request::instance()->post('level/d', 0, 'trim');//背景id
            $gender= Request::instance()->post('gender/d', 0, 'trim');//性别
            if (empty($phone) || empty($code) || empty($course) || empty($name)) {
                throw new Exception('参数缺失', '102');
            }
            //检查验证码
            $sms = \think\Loader::model('Sms','service');
            $data = $sms->checkCode($phone, $code);
            //添加预约信息
            $model = \think\Loader::model('Student','service');
            $data = $model->appointment($name, $phone, $course, $gender, $openid, $level, $comment);
            return parent::returnTrue($data);
        } catch (Exception $e) {
            $message['code'] = $e->getCode();
            $message['mes'] = $e->getMessage();
            return parent::returnFalse($message);
        }
    }
}
